package abaco.symbol.operation.arithmetic;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;

import abaco.Symbol;
import abaco.gui.Menu;
import abaco.gui.Menuable;
import abaco.gui.Panel;
import abaco.symbol.Text;
import abaco.symbol.operation.Arithmetic;

public class Division extends Arithmetic implements Menuable {
	public static final String DIVIDEND = "operation-binary-0";
	public static final String DIVISOR = "operation-binary-1";
	protected Menu menu = new Menu();
	
	public Division() {
		minParameters = 2;
		maxParameters = 2;
		setSymbolChild(new Text(), DIVISOR);
		setSymbolChild(new Text(), DIVIDEND);
		buildCustonMenu();
	}
	/**
	 * invert DIVISOR to DIVIDEND
	 */
	protected void invert() {
		System.out.println("invert actived");
	}
	/**
	 * 
	 */
	public void buildCustonMenu() {
		JMenuItem invert = new JMenuItem("Invert");
		invert.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent ae) {  
                invert();  
            }
        });
		JMenuItem latex = new JMenuItem("Latex view");
		latex.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent ae) {  
            	JOptionPane.showMessageDialog(null, toLatex());  
            }
        });
		menu.add(invert);
		menu.add(latex);
	}
	
	@Override
	public Menu getMenu() {
		// TODO Auto-generated method stub
		return menu;
	}
	
	@Override
	public Symbol buildPanel(Panel panel) {
		checkChildren(true);
		panel.setLayout(new GridBagLayout());

		Panel dividendPanel = getChildPanel(DIVIDEND, false);
		GridBagConstraints dividendConstraints = new GridBagConstraints();
		dividendConstraints.insets = new Insets(0, 0, 5, 0);
		dividendConstraints.gridx = 0;
		dividendConstraints.gridy = 0;
		panel.add(dividendPanel, dividendConstraints);

		JSeparator separator = new JSeparator();
		separator.setForeground(Color.BLACK);
		GridBagConstraints separatorConstraints = new GridBagConstraints();
		separatorConstraints.gridx = 0;
		separatorConstraints.gridy = 1;
		separatorConstraints.fill  = GridBagConstraints.HORIZONTAL;
		panel.add(separator, separatorConstraints);

		Panel divisorPanel = getChildPanel(DIVISOR, false);
		GridBagConstraints divisorConstraints  = new GridBagConstraints();
		divisorConstraints.insets = new Insets(5, 0, 0, 0);
		divisorConstraints.gridx  = 0;
		divisorConstraints.gridy  = 2;
		panel.add(divisorPanel, divisorConstraints);

		return this;
	}

	@Override
	public String toLatex() {
		String parameters = super.toLatex();
		if (parameters.equals(UNDEFINED_LATEX)) {
			return parameters;
		}
		StringBuilder latex = new StringBuilder();
		latex.append("{\\frac");
		// remove parent brackets
		latex.append(parameters.substring(1, parameters.length() - 2));
		latex.append("}");
		return latex.toString();
	}
}
